package cn.tedu._01yuncat.controller;

import cn.tedu._01yuncat.base.response.JsonResult;
import cn.tedu._01yuncat.base.response.StatusCode;
import cn.tedu._01yuncat.mapper.VehicleMapper;
import cn.tedu._01yuncat.pojo.dto.VehicleAddParam;
import cn.tedu._01yuncat.pojo.dto.VehicleListQuery;
import cn.tedu._01yuncat.pojo.entity.Vehicle;
import cn.tedu._01yuncat.pojo.vo.UserVO;
import cn.tedu._01yuncat.pojo.vo.VehicleAllListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "车辆管理模块")
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession  session){
        log.debug("开始处理【添加车辆】的请求，参数：{}",vehicleAddParam);
        UserVO userVO = (UserVO)session.getAttribute("user");

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        Long id = userVO.getId();
        vehicle.setUserId(id);
        log.debug("即将添加车辆：{}",vehicle);
        int result = vehicleMapper.addVehicle(vehicle);
        if (result > 0){
            return JsonResult.ok();
        }
        return JsonResult.fail(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand",description = "车辆品牌"),
            @Parameter(name = "license",description = "车辆车牌"),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult vehicleAllList(VehicleListQuery vehicleListQuery, HttpSession session){
        log.debug("开始处理【查询车辆列表】的请求，参数：{}",vehicleListQuery);
        UserVO userVO = (UserVO)session.getAttribute("user");
        List<VehicleAllListVO> listVO = vehicleMapper.vehicleAllList(vehicleListQuery);
        return JsonResult.ok(listVO);
    }

    @PostMapping("del{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult vehicleDel(@PathVariable Long id, HttpSession session){
        log.debug("开始处理【删除车辆】的请求，参数：{}",id);
        UserVO userVO = (UserVO)session.getAttribute("user");

        Long userIdVO = userVO.getId();
        int result = vehicleMapper.vehicleDelById(id,userIdVO);
        if (result != 1){
            return JsonResult.fail();
        }
        return JsonResult.ok();
    }

}
